﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Trippy.Levels;
using Trippy.CollidableGameComponents.Objects;
using Trippy.Animation;
using Trippy.CollidableGameComponents.Agents;


namespace Trippy.CollidableGameComponents.Events
{
    public class EventManager : GameComponent
    {
        public delegate void EventHandler();

        private SortedList<double, EventHandler> queue = new SortedList<double, EventHandler>();

        private double currentTime;
        public Level CurrentLevel;

        public EventManager(Level level) : base(level.Game)
        {
            CurrentLevel = level;
        }

        public override void Update(GameTime gameTime)
        {
            currentTime = Convert.ToDouble(gameTime.TotalGameTime.TotalSeconds);

            while (queue.Count > 0 && queue.First().Key < currentTime)
            {
                queue.First().Value();
                queue.Remove(queue.First().Key);
            }

            base.Update(gameTime);
        }

        public void Schedule(EventHandler handler, double timeFromNow)
        {
            double time = currentTime + timeFromNow;
            while (queue.ContainsKey(time))
            {
                time += .00001;
            }
            queue.Add(time, handler);
        }
    }
}
